home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / mint96sb.zoo / src / asm.y next >
Encoding:
Text File  |  1992-10-21  |  5.6 KB  |  266 lines

  1. /* Infix notation calculator--calc */
  2.  
  3. %{
  4. #define YYSTYPE char *
  5.  
  6. #include "asmtrans.h"
  7. %}
  8.  
  9. %token WORD
  10. %token WHITESP
  11. %token EOLN
  12. %token STRING
  13. %token DEFINECMD
  14. %token INCLUDECMD
  15. %token IFDEFCMD
  16. %token IFNDEFCMD
  17. %token ELSECMD
  18. %token ENDIFCMD
  19.  
  20. /* Grammar follows */
  21. %%
  22. input:
  23.         | input line
  24. ;
  25.  
  26. line:    EOLN        { emit($1); }
  27.         /* note that emit() will automatically free the memory,
  28.            and will also check the hidecnt variable */
  29.     | label EOLN    { emit($1); emit($2); }
  30.     | opline EOLN    { emit($1); emit($2); }
  31.     | label opline EOLN    { emit($1); emit($2);
  32.             emit($3); }
  33.     | INCLUDECMD WHITESP STRING EOLN { if (!hidecnt) do_include($3); free($3); }
  34.     | INCLUDECMD WHITESP WORD EOLN { if (!hidecnt) do_include($3); free($3); }
  35.     | DEFINECMD WHITESP WORD WHITESP STRING EOLN {
  36.         if (!hidecnt) do_define($3, $5); free($3); free($5); }
  37.     | DEFINECMD WHITESP WORD WHITESP operand EOLN {
  38.         if (!hidecnt) do_define($3, $5); free($3); free($5); }
  39.     | IFDEFCMD WHITESP WORD EOLN { do_ifdef($3); free($3); }
  40.     | IFNDEFCMD WHITESP WORD EOLN { do_ifndef($3); free($3); }
  41.     | ELSECMD EOLN { do_else(); }
  42.     | ENDIFCMD EOLN { do_endif(); } 
  43. ;
  44.  
  45. opline:    WHITESP opcode        { $$ = do_ops("", $2, "", ""); free($2); }
  46.     | WHITESP opcode WHITESP ops
  47.             { $$ = do_ops("", $2, $3, $4);
  48.              free($2); free($3); free($4); }
  49.     | WORD WHITESP opcode    { $$ = do_ops($1, $3, "", ""); free($1); free($3); }
  50.     | WORD WHITESP opcode WHITESP ops
  51.             { $$ = do_ops($1, $3, $4, $5);
  52.              free($1); free($3); free($4); free($5);}
  53. ;
  54.  
  55. ops: operand { $$ = $1; }
  56.     | operand ',' ops { $$ = concat3($1, ",", $3);
  57.                 free($1); free($3); }
  58. ;
  59.  
  60. opcode: WORD    { $$ = wordlookup($1); free($1); }
  61. ;
  62.  
  63. label: WORD ':' { $$ = concat($1, ":"); free($1); }
  64.  
  65. operand: basic    {$$ = $1; }
  66.     | '#' basic {$$ = immediate($2); free($2); }
  67.     | '(' basic ')' {$$ = indirect($2); free($2); }
  68.     | '(' basic ')' '+' {$$ = postinc($2); free($2); }
  69.     | '-' '(' basic ')' {$$ = predec($3); free($3); }
  70.     | basic '(' basic ')' {$$ = indexed($1, $3); free($1); free($3); }
  71.     | '(' basic ')' WORD {$$ = sizedop($2, $4); free($2); free($4); }
  72.     | basic '(' basic ',' basic ')' {$$ = twoindex($1, $3, $5);
  73.                 free($1); free($3); free($5); }
  74.     | basic '{' basic ':' basic '}' {$$ = bitfield($1, $3, $5);
  75.             free($1); free($3); free($5); }
  76. ;
  77.  
  78. basic: basexpr { $$ = $1; }
  79.     | basexpr op basic { $$ = concat3($1, $2, $3); free($1); free($2); free($3); }
  80.     | '-' basic { $$ = concat("-", $2); free($2); }
  81.  
  82. basexpr: WORD {$$ = wordlookup($1); free($1); }
  83.     | '$' WORD {$$ = hexop($2); free($2);}
  84. ;
  85.  
  86. op:     '+' { $$ = strdup("+"); }
  87.     | '-' { $$ = strdup("-"); }
  88.     | '*' { $$ = strdup("*"); }
  89.     | '/' { $$ = strdup("/"); }
  90. ;
  91. %%
  92. #include <setjmp.h>
  93.  
  94. jmp_buf start;
  95.  
  96. #ifdef NATIVEATARI
  97. #define STACK 32*1024L;
  98. #ifdef LATTICE
  99. long _STACK = STACK;
  100. #endif
  101. #ifdef __GNUC__
  102. long _stksize = STACK;
  103. #endif
  104.  
  105. static void
  106. hit_return()
  107. {
  108.     printf("Hit return to continue\n");
  109.     fflush(stdout);
  110.     getchar();
  111. }
  112. #endif
  113.  
  114. void usage()
  115. {
  116.     fprintf(stderr, "Usage: asmtrans [-gas][-asm][-o outfile] infile\n");
  117.     exit(2);
  118. }
  119.  
  120. int errors = 0;
  121.  
  122. void
  123. do_include(file)
  124.     char *file;
  125. {
  126.     jmp_buf save;
  127.     FILE *oldin, *f;
  128.  
  129.     f = fopen(file, "r");
  130.     if (!f) {
  131.         perror(file);
  132.         return;
  133.     }
  134.     bcopy(start, save, sizeof(jmp_buf));
  135.     oldin = infile;
  136.     infile = f;
  137.     setjmp(start);
  138.     yyparse();
  139.     fclose(f);
  140.     infile = oldin;
  141.     bcopy(save, start, sizeof(jmp_buf));
  142.     longjmp(start,1);
  143. }
  144.  
  145. /* set up initial definitions based on syntax type */
  146.  
  147. void
  148. do_initial_defs()
  149. {
  150.     if (syntax == GAS) {
  151.         do_define("fpiar", "fpi");
  152.         do_define("XREF", ".globl");
  153.         do_define("XDEF", ".globl");
  154.         do_define("TEXT", ".text");
  155.         do_define("DATA", ".data");
  156.     /* gas doesn't have a .bss directive */
  157.         do_define("BSS", ".data");
  158.         do_define("END", "| END");
  159.         do_define("dc.l", ".long");
  160.         do_define("dc.w", ".word");
  161.         do_define("dc.b", ".byte");
  162.     } else if (syntax == ASM) {
  163.         do_define("TEXT", "SECTION TEXT");
  164.         do_define("DATA", "SECTION DATA");
  165.         do_define("BSS", "SECTION BSS");
  166.     }
  167. }
  168.  
  169. int
  170. main (argc, argv)
  171.     int argc; char **argv;
  172. {
  173.     FILE *f;
  174. #ifdef NATIVEATARI
  175.     if (!argv[0] || !argv[0][0])    /* run from desktop? */
  176.         atexit(hit_return);
  177. #endif
  178.     argv++;
  179.     outfile = stdout;
  180.  
  181.     while (*argv) {
  182.         if (!strcmp(*argv, "-o")) {
  183.             argv++;
  184.             if (*argv == 0) {
  185.                 fprintf(stderr, "missing argument to -o\n");
  186.                 usage();
  187.             }
  188.             f = fopen(*argv, "w");
  189.             if (!f)
  190.                 perror(*argv);
  191.             else
  192.                 outfile = f;
  193.             argv++;
  194.         } else if (!strcmp(*argv, "-gas")) {
  195.             argv++;
  196.             syntax = GAS;
  197.         } else if (!strcmp(*argv, "-asm")) {
  198.             argv++;
  199.             syntax = ASM;
  200.         } else if (!strcmp(*argv, "-purec")) {
  201.             argv++;
  202.             syntax = PUREC;
  203.         } else if (!strncmp(*argv, "-D", 2)) {
  204.             char *word, *defn;
  205.             word = *argv+2;
  206.             defn = index(word,'=');
  207.             if (defn)
  208.                 *defn++ = '\0';
  209.             else
  210.                 defn = "1";
  211.             if (*word) do_define(word,defn);
  212.             argv++;
  213.         } else if (!strcmp(*argv, "--")) {
  214.             argv++;
  215.             break;
  216.         } else {
  217.             if (**argv == '-') {
  218.                 fprintf(stderr, "unknown option: %s\n",
  219.                     *argv);
  220.                 usage();
  221.             }
  222.             break;
  223.         }
  224.     }
  225.  
  226.     do_initial_defs();
  227.  
  228.     if (*argv == 0) {
  229.         setjmp(start);
  230.         infile = stdin;
  231.         yyparse();
  232.     } else {
  233.         while(*argv) {
  234.         if (!(f = fopen(*argv, "r")))
  235.             perror(*argv);
  236.         else {
  237.             infile = f;
  238.             setjmp(start);
  239.             yyparse();
  240.             fclose(f);
  241.         }
  242.         argv++;
  243.         }
  244.     }
  245.  
  246.     if (ifstkptr != 0) {
  247.         fputs("%ifdef without matching %endif\n", stderr);
  248.         errors++;
  249.     }
  250.     return errors;
  251. }
  252.  
  253. void
  254. yyerror (s)  /* Called by yyparse on error */
  255.      char *s;
  256. {
  257.     errors++;
  258.     printf("%s\n", s);
  259.     longjmp(start, 1);
  260. }
  261.  
  262. void dbgmsg(s) char *s;
  263. {
  264.     fprintf(stderr, "%s\n", s);
  265. }
  266.